require 'lib/xmpp'
require 'rubygems'
require 'xmpp4r'
require 'xmpp4r-simple'
require 'json'

class XmppController < ActionController::Base
    def index
        login
        render :action => 'login'
    end

    def login
        if params[:jid].nil? or params[:password].nil?
            flash[:notice] = "Jid or password not supplied" 
        else
            session = Xmpp::JabberSession.new(params[:jid], params[:password])
            if not session
                flash[:notice] = "Failed to authenticate"
            end
            redirect_to :action => 'roster', :uuid => session.uuid
        end
    end

    def roster
        if params[:uuid]
            #@roster_text = "{'uuid': #{params[:uuid]}}"
            session = Xmpp::Workers[params[:uuid]]

            if session
                context = {'uuid' => session.uuid,
                           'roster' => session.get_roster().items.keys
                          }
                @context = context.to_json
            else
                flash[:notice] = "No such session"
                context = {'error' => 'no such session'}
                @context = context.to_json
            end
        end
    end

    def message
        if params[:uuid] and params[:message] and params[:jid]
            session = Xmpp::Workers[params[:uuid]]

            if session
                context = {'uuid' => session.uuid,
                           'message' => params[:message],
                           'jid' => params[:jid],
                           'return_code' => session.send(params[:jid], params[:message])
                          }
                
                @context = context.to_json
            else
                flash[:notice] = "No such session"
                context = {'error' => 'no such session'}
                @context = context.to_json
            end
        end
    end

    def add
        if params[:uuid] and params[:jid]
            session = Xmpp::Workers[params[:uuid]]

            if session
                context = {'uuid' => session.uuid,
                           'jid' => params[:jid],
                           'return_code' => session.add(params[:jid]),
                           'roster' => session.get_roster().items.keys,
                          }
                @context = context.to_json
            else
                flash[:notice] = "No such session"
                context = {'error' => 'no such session'}
                @context = context.to_json
            end
        end
    end

    def remove
        if params[:uuid] and params[:jid]
            session = Xmpp::Workers[params[:uuid]]

            if session
                context = {'uuid' => session.uuid,
                           'jid' => params[:jid],
                           'return_code' => session.remove(params[:jid]),
                           'roster' => session.get_roster().items.keys,
                          }
                @context = context.to_json
            else
                flash[:notice] = "No such session"
                context = {'error' => 'no such session'}
                @context = context.to_json
            end
        end
    end
end
